$(function(){
	var page = 1;
	//表情js
	$("body").on('click',".f-face",function(){
		var comment_id = $(this).attr('comment_id');
		$('#f-biaoqing-'+comment_id).toggle();
	});
	//添加表情
	$("body").on('click',".f-biaoqing img",function(){
		var comment_id = $(this).parent().attr('comment_id');
		var img = $(this).prop("outerHTML");
		var comment = $('#f-comment-textarea-'+comment_id).html();
		$('#f-comment-textarea-'+comment_id).html(comment + img);
		$('#f-comment-textarea-'+comment_id).focus();
	});
	
	$('#getMoreArticles').click(function(){
		var article_id = $(this).attr('article_id');
		layer.load(1);
		page = page + 1;
		$.ajax({
			url: web_site+'/Comment/getComment.html',
			type: "post",
			data:{article_id:article_id,page:page},
			dataType: 'json',
			success: function(result){
				layer.closeAll('loading');
				var html = "";
				for(var i = 0;i < result.list.length;i++){
					html += '<div id="comment-id" class="row f-user f-comment-parent">'
				  		 + '<div class="col-xs-2 col-sm-1 col-md-1 col-lg-1 f-pic-content">'
				  		 + '<img class="f-user-pic" src="'+result.list[i].head_imgurl+'" alt="'+result.website_info.website_name+'" title="'+result.website_info.website_name+'">'
				  		 + '</div>'
				  		 + '<div class="col-xs-10 col-sm-11 col-md-11 col-lg-11">'
				  		 	+ '<p class="f-user-content">'
				  		 		+ '<span class="f-user-name">'+result.list[i].nick_name+'</span>'
				  		 		+ '<span class="f-user-comment">'+result.list[i].content+'</span>'
				  		 	+ '</p>'
				  		 	+ '<p class="f-user-date">'+result.list[i].add_time
				  		 		+ '<a href="javascript:void(0)" article_id="'+result.article_id+'" comment_id="'+result.list[i].id+'" username="'+result.list[i].nick_name+'" onclick="reply_comment(this)">回复</a>'
				  		 	+ '</p>'
				  		 + '</div>'
				  		 + '</div>';
				}
				$('.f-more').before(html);
				if(result.is_exist == 0){
					$('.f-more').toggle();
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.status);
			}
		});
	});
});

function formatDate(time){ 
	return new Date(parseInt(time) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");      
}

//评论文章
function comment(obj){
	if(c_mid == ''){
		layer.msg('请先登录后留言', {icon: 5,time: 2000});
		return false;
	}else{
		var article_id = $(obj).attr('article_id');
		var comment = $('#f-comment-textarea-0').html();
		if($.trim(comment) != '' && article_id != ''){
			layer.load(1);
			$.ajax({
				url: web_site+'/Comment/addComment.html',
				type: "post",
				data:{article_id:article_id,comment:comment},
				dataType: 'json',
				success: function(result){
					layer.closeAll('loading');
					if(result.error == 1){
						layer.msg(result.msg, {icon: 5});
					}else{
						var html = '<div id="comment-'+result.comment_info.id+'" class="row f-user f-comment-parent">'
						  		 + '<div class="col-xs-2 col-sm-1 col-md-1 col-lg-1 f-pic-content">'
						  		 + '<img class="f-user-pic" src="'+result.member_info.head_imgurl+'" alt="'+result.website_info.website_name+'" title="'+result.website_info.website_name+'">'
						  		 + '</div>'
						  		 + '<div class="col-xs-10 col-sm-11 col-md-11 col-lg-11">'
						  		 	+ '<p class="f-user-content">'
						  		 		+ '<span class="f-user-name">'+result.member_info.nick_name+'</span>'
						  		 		+ '<span class="f-user-comment">'+result.comment_info.content+'</span>'
						  		 	+ '</p>'
						  		 	+ '<p class="f-user-date" id="f-user-date-'+result.comment_info.id+'">'+result.comment_info.add_time
						  		 		+ '<a href="javascript:void(0)" article_id="'+article_id+'" comment_id="'+result.comment_info.id+'" uid="'+result.comment_info.member_id+'" username="'+result.member_info.nick_name+'" onclick="show_comment(this)">回复</a>'
						  		 	+ '</p>'
						  		 + '</div>'
						  		 + '</div>';
						$('.f-comment-user').prepend(html);
						$('.f-comment-textarea').html('');
						$('#f-biaoqing-0').hide();
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert(XMLHttpRequest.status);
	                //alert(XMLHttpRequest.readyState);
	                //alert(XMLHttpRequest.responseText);
				}
			});
		}else{
			return false;
		}
	}
}
//显示评论框
function show_comment(obj){
	if(c_mid == ''){
		layer.msg('请先登录后留言', {icon: 5,time: 2000});
		return false;
	}else{
		var uid = $(obj).attr('uid');
		if(uid != c_mid){
			var article_id = $(obj).attr('article_id');
			var comment_id = $(obj).attr('comment_id');
			var pid = comment_id;
			if($(obj).attr('pid')){
				var pid = $(obj).attr('pid');
			}
			var comment_obj = $('#f-comment-content-'+comment_id);
			if(comment_obj.length > 0){
				comment_obj.remove();
			}else{
				var html = '<div class="f-comment-content" id="f-comment-content-'+comment_id+'" comment_id="'+comment_id+'">'
				 + '<div class="f-comment-textarea" id="f-comment-textarea-'+comment_id+'" contenteditable="true"></div>'
				 + '<div class="f-comment-submit">'
				 + '	<div class="f-emote">'
				 + '		<img class="f-face" id="f-face-'+comment_id+'" comment_id="'+comment_id+'" src="'+img_url+'/home/smile.png"/>'
				 + '		<div class="f-biaoqing" id="f-biaoqing-'+comment_id+'" comment_id="'+comment_id+'" style="display: none;">'
				 + emote_html
				 + '		</div>'
				 + '	</div>'
				 + '	<div class="f-submit-button">'
				 + '		<input type="button" class="btn btn-success" value="评 论" article_id="'+article_id+'" comment_id="'+comment_id+'" pid="'+pid+'" onclick="reply_comment(this)">'
				 + '		</div>'
				 + '	</div>'
				 + '</div>';
				$('#f-user-date-'+comment_id).after(html);
			}
		}
	}
}
//回复评论
function reply_comment(obj){
	if(c_mid == ''){
		layer.msg('请先登录后留言', {icon: 5,time: 2000});
		return false;
	}else{
		var article_id = $(obj).attr('article_id');
		var comment_id = $(obj).attr('comment_id');
		var pid = $(obj).attr('pid');
		var comment = $('#f-comment-textarea-'+comment_id).html();
		layer.load(1);
		$.ajax({
			url: web_site+'/Comment/replyComment.html',
			type: "post",
			data:{article_id:article_id,pid:comment_id,comment:comment},
			dataType: 'json',
			success: function(result){
				layer.closeAll('loading');
				if(result.error == 1){
					layer.msg(result.msg, {icon: 5});
				}else{
					console.log(result);
					$("div").remove('#f-comment-content-'+comment_id);
					var html = '<div id="comment-'+result.comment_info.id+'" class="row f-user f-comment-child">'
	  						 	+ '<div class="col-xs-2 col-sm-1 col-md-1 col-lg-1 f-pic-content">'
	  						 		+ '<img class="f-user-pic" src="'+result.member_info.head_imgurl+'" alt="'+result.website_info.website_name+'" title="'+result.website_info.website_name+'">'
	  						 	+ '</div>'
	  						 	+ '<div class="col-xs-10 col-sm-11 col-md-11 col-lg-11">'
	  						 		+ '<p class="f-user-content">'
	  						 			+ '<span class="f-reply-name">'+result.member_info.nick_name+'</span>&nbsp;'
	  						 			+ '<span class="f-reply">回复</span>&nbsp;'
	  						 			+ '<span class="f-user-name">'+result.f_user_name+'</span>&nbsp;'
	  						 			+ '<span class="f-user-comment">'+result.comment_info.content+'</span>'
	  						 		+ '</p>'
	  						 		+ '<p class="f-user-date" id="f-user-date-'+result.comment_info.id+'">'
	  						 			+ result.comment_info.add_time
	  						 			+ '<a href="javascript:void(0)" article_id="'+article_id+'" comment_id="'+result.comment_info.id+'" pid="'+result.comment_info.pid+'" uid="'+result.comment_info.member_id+'" username="'+result.member_info.nick_name+'" onclick="show_comment(this)">回复</a>'
	  						 			+ '</p>'
	  						 	+ '</div>'
	  						 + '</div>';
					if(pid != comment_id){
						$('#comment-'+comment_id).parent().children(".f-comment-child").last().after(html);
					}else{
						$('#f-user-date-'+comment_id).after(html);
					}
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.status);
	            //alert(XMLHttpRequest.readyState);
	            //alert(XMLHttpRequest.responseText);
			}
		});
	}
}